package com.example.administrator.exkotlin.endurance

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.widget.Toast

/**
 * 实现sqllite数据库对象
 * */
class MyDatabaseHelper(val context: Context, name: String, version: Int) :
        SQLiteOpenHelper(context, name, null, version) {
    //建表语句
    private val createBookDb = "create table Book (" + " id integer primary key autoincrement," +
            "author text," +
            "price real," +
            "pages integer," +
            "name text)"

    //在第一次创建数据库的时候会被调用
    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(createBookDb)
        Toast.makeText(context,"数据库建表成功",Toast.LENGTH_SHORT).show()
    }

    //数据库升级的时候会被调用
    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        Toast.makeText(context,"数据库升级:旧版本：$oldVersion   新版本：$newVersion",Toast.LENGTH_SHORT).show()
    }
}